home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / gdb / environ.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  4KB  |  188 lines

  1. /* environ.c -- library for manipulating environments for GNU.
  2.    Copyright (C) 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #define min(a, b) ((a) < (b) ? (a) : (b))
  19. #define max(a, b) ((a) > (b) ? (a) : (b))
  20.  
  21. #include "environ.h"
  22. #include <string.h>
  23. #include "defs.h" /* For strsave().  */
  24.  
  25. extern void free ();
  26.  
  27. /* Return a new environment object.  */
  28.  
  29. struct environ *
  30. make_environ ()
  31. {
  32.   register struct environ *e;
  33.  
  34.   e = (struct environ *) xmalloc (sizeof (struct environ));
  35.  
  36.   e->allocated = 10;
  37.   e->vector = (char **) xmalloc ((e->allocated + 1) * sizeof (char *));
  38.   e->vector[0] = 0;
  39.   return e;
  40. }
  41.  
  42. /* Free an environment and all the strings in it.  */
  43.  
  44. void
  45. free_environ (e)
  46.      register struct environ *e;
  47. {
  48.   register char **vector = e->vector;
  49.  
  50.   while (*vector)
  51.     free (*vector++);
  52.  
  53.   free (e);
  54. }
  55.  
  56. /* Copy the environment given to this process into E.
  57.    Also copies all the strings in it, so we can be sure
  58.    that all strings in these environments are safe to free.  */
  59.  
  60. void
  61. init_environ (e)
  62.      register struct environ *e;
  63. {
  64.   extern char **environ;
  65.   register int i;
  66.  
  67.   for (i = 0; environ[i]; i++) /*EMPTY*/;
  68.  
  69.   if (e->allocated < i)
  70.     {
  71.       e->allocated = max (i, e->allocated + 10);
  72.       e->vector = (char **) xrealloc ((char *)e->vector,
  73.                       (e->allocated + 1) * sizeof (char *));
  74.     }
  75.  
  76.   bcopy (environ, e->vector, (i + 1) * sizeof (char *));
  77.  
  78.   while (--i >= 0)
  79.     {
  80.       register int len = strlen (e->vector[i]);
  81.       register char *new = (char *) xmalloc (len + 1);
  82.       bcopy (e->vector[i], new, len + 1);
  83.       e->vector[i] = new;
  84.     }
  85. }
  86.  
  87. /* Return the vector of environment E.
  88.    This is used to get something to pass to execve.  */
  89.  
  90. char **
  91. environ_vector (e)
  92.      struct environ *e;
  93. {
  94.   return e->vector;
  95. }
  96.  
  97. /* Return the value in environment E of variable VAR.  */
  98.  
  99. char *
  100. get_in_environ (e, var)
  101.      struct environ *e;
  102.      char *var;
  103. {
  104.   register int len = strlen (var);
  105.   register char **vector = e->vector;
  106.   register char *s;
  107.  
  108.   for (; s = *vector; vector++)
  109.     if (!strncmp (s, var, len)
  110.     && s[len] == '=')
  111.       return &s[len + 1];
  112.  
  113.   return 0;
  114. }
  115.  
  116. /* Store the value in E of VAR as VALUE.  */
  117.  
  118. void
  119. set_in_environ (e, var, value)
  120.      struct environ *e;
  121.      char *var;
  122.      char *value;
  123. {
  124.   register int i;
  125.   register int len = strlen (var);
  126.   register char **vector = e->vector;
  127.   register char *s;
  128.  
  129.   for (i = 0; s = vector[i]; i++)
  130.     if (!strncmp (s, var, len)
  131.     && s[len] == '=')
  132.       break;
  133.  
  134.   if (s == 0)
  135.     {
  136.       if (i == e->allocated)
  137.     {
  138.       e->allocated += 10;
  139.       vector = (char **) xrealloc ((char *)vector,
  140.                        (e->allocated + 1) * sizeof (char *));
  141.       e->vector = vector;
  142.     }
  143.       vector[i + 1] = 0;
  144.     }
  145.   else
  146.     free (s);
  147.  
  148.   s = (char *) xmalloc (len + strlen (value) + 2);
  149.   strcpy (s, var);
  150.   strcat (s, "=");
  151.   strcat (s, value);
  152.   vector[i] = s;
  153.  
  154.   /* Certain variables get exported back to the parent (e.g. our) 
  155.      environment, too.  */
  156.   if (!strcmp(var, "PATH")            /* Object file location */
  157.    || !strcmp (var, "G960BASE")         /* Intel 960 downloads */
  158.    || !strcmp (var, "G960BIN")             /* Intel 960 downloads */
  159.    || !strcmp (var, "GNUTARGET")        /* BFD object file type */
  160.                 ) {
  161.     putenv (strsave (s));
  162.   }
  163.   return;
  164. }
  165.  
  166. /* Remove the setting for variable VAR from environment E.  */
  167.  
  168. void
  169. unset_in_environ (e, var)
  170.      struct environ *e;
  171.      char *var;
  172. {
  173.   register int len = strlen (var);
  174.   register char **vector = e->vector;
  175.   register char *s;
  176.  
  177.   for (; s = *vector; vector++)
  178.     if (!strncmp (s, var, len)
  179.     && s[len] == '=')
  180.       {
  181.     free (s);
  182.     bcopy (vector + 1, vector,
  183.            (e->allocated - (vector - e->vector)) * sizeof (char *));
  184.     e->vector[e->allocated - 1] = 0;
  185.     return;
  186.       }
  187. }
  188.